package com.shawn.mobliesafe;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.ProgressBar;

import com.shawn.engine.SMSEngine;
import com.shawn.utils.MyAsycnTaks;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class AToolsActivity extends AppCompatActivity {

    private ProgressBar pb_atools_sms;
    private String smsPackage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atools);

        pb_atools_sms = (ProgressBar)findViewById(R.id.pb_atools_sms);


    }

    public void queryaddress(View view){
        Intent intent = new Intent(this,AddressActivity.class);
        startActivity(intent);
    }

    /**
     * 备份短信
     * @param view
     */
    public void backupsms(View view) {
        //pb_atools_sms.setVisibility(View.VISIBLE);
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        new Thread() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                SMSEngine.getAllSMS(AToolsActivity.this, new SMSEngine.ShowProgress() {
                    @Override
                    public void setMax(int max) {
                        dialog.setMax(max);
                    }

                    @Override
                    public void setProgress(final int progress) {
                        dialog.setProgress(progress);
                    }
                });
                dialog.dismiss();
            }
        }.start();

    }

    /**
     * 短信还原
     * @param view
     */
    public void restoresms(View view) {
        smsPackage = Telephony.Sms.getDefaultSmsPackage(AToolsActivity.this);
        final String packageName = AToolsActivity.this.getPackageName();
        if (!smsPackage.equals(packageName)) {
            //如果这个App不是默认的Sms App，则修改成默认的SMS APP
            //因为从Android 4.4开始，只有默认的SMS APP才能对SMS数据库进行处理
            Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName);
            startActivityForResult(intent,0);
        }


       /* smsPackage = Telephony.Sms.getDefaultSmsPackage(AToolsActivity.this);
        final String packageName = AToolsActivity.this.getPackageName();
        if (!smsPackage.equals(packageName)) {
            //如果这个App不是默认的Sms App，则修改成默认的SMS APP
            //因为从Android 4.4开始，只有默认的SMS APP才能对SMS数据库进行处理
            Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName);
            startActivityForResult(intent,0);;
        }*/
    }

    //@Override
    /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ContentResolver resolver = getContentResolver();
        Uri uri = Uri.parse("content://sms");
        ContentValues values = new ContentValues();
        values.put("address", "95588");
        values.put("date", System.currentTimeMillis());
        values.put("type", 1);
        values.put("body", "zhuan zhang le $10000000000000000000");
        resolver.insert(uri, values);

        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, smsPackage);
        startActivity(intent);
    }*/

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        new Thread() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                SMSEngine.restoreAllSMS(AToolsActivity.this, new SMSEngine.ShowProgress() {
                    @Override
                    public void setMax(int max) {
                        dialog.setMax(max);
                    }

                    @Override
                    public void setProgress(int progress) {
                        dialog.setProgress(progress);
                    }
                });
                dialog.dismiss();
                Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, smsPackage);
                startActivity(intent);
            }
        }.start();
    }
}
